package com.xiaoxing.salesclient.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.xiaoxing.gifloadingview.LoadingDialogUtil;
import com.xiaoxing.salesclient.di.component.DaggerChanPinXiangQingComponent;
import com.xiaoxing.salesclient.di.module.ChanPinXiangQingModule;
import com.xiaoxing.salesclient.mvp.contract.ChanPinXiangQingContract;
import com.xiaoxing.salesclient.mvp.model.entity.OrderBuy;
import com.xiaoxing.salesclient.mvp.model.entity.ProductDetail;
import com.xiaoxing.salesclient.mvp.presenter.ChanPinXiangQingPresenter;
import com.xiaoxing.salesclient.mvp.utils.GlideImageLoader;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonres.view.AmountView;
import me.jessyan.armscomponent.commonsdk.core.BaseConstants;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.armscomponent.commonsdk.utils.WebviewUtil;
import xiaoxing.com.salesclient.R;
import xiaoxing.com.salesclient.R2;

import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.SALES_CLIENT_CHAN_PIN_XIANG_QING_ACTIVITY)
public class ChanPinXiangQingActivity extends BaseActivity<ChanPinXiangQingPresenter> implements ChanPinXiangQingContract.View {


    @Autowired
    String product_id;

    @BindView(R2.id.convenientBanner)
    Banner banner;
    @BindView(R2.id.tv_good_name)
    TextView tvGoodName;
    @BindView(R2.id.tv_dan_wei)
    TextView tvDanWei;
    @BindView(R2.id.tv_price)
    TextView tvPrice;
    @BindView(R2.id.tv_yuan_jia)
    TextView tvYuanJia;
    @BindView(R2.id.amount_view)
    AmountView mAmountView;
    @BindView(R2.id.tv_chan_pin_shuo_ming)
    WebView tvChanPinShuoMing;
    @BindView(R2.id.btn_ding_gou)
    Button btnDingGou;
    @BindView(R2.id.btn_qu_xiao)
    Button btnQuXiao;

    private int buyNum = 1;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerChanPinXiangQingComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .chanPinXiangQingModule(new ChanPinXiangQingModule(this))
                .build()
                .inject(this);

        ARouter.getInstance().inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_chan_pin_xiang_qing; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, "詳情");

        mAmountView.setGoods_storage(50);
        mAmountView.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, int amount) {
                buyNum = amount;
            }
        });

        getProductDetail();
    }

    private void getProductDetail() {
        mPresenter.productDetail(product_id);
    }

    @Override
    public void showLoading() {

        LoadingDialogUtil.showGifdialog2(getSupportFragmentManager(), R.drawable.public_loading);
    }

    @Override
    public void hideLoading() {
        LoadingDialogUtil.dismissDialog();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();


    }

    //如果你需要考虑更好的体验，可以这么操作
    @Override
    protected void onStart() {
        super.onStart();
        //开始轮播
        banner.startAutoPlay();
    }

    @Override
    protected void onStop() {
        super.onStop();
        //结束轮播
        banner.stopAutoPlay();
    }

    @Override
    public void productDetailSuccess(ProductDetail productDetail) {


        if (productDetail != null && productDetail.getData() != null) {

            ProductDetail.DataBean dataBean = productDetail.getData();
            setBannerData(dataBean);

            tvGoodName.setText(dataBean.getProduct_name());
            tvDanWei.setText(dataBean.getUnit());
            tvPrice.setText(dataBean.getPrice() + "元");
            tvYuanJia.setText(dataBean.getPrice1() + "元");
            WebviewUtil.loadData(this, tvChanPinShuoMing, dataBean.getInfo());
        }
    }

    @Override
    public void orderBuySuccess(OrderBuy orderBuy) {


        if (orderBuy != null && orderBuy.getData() != null) {
            ArmsUtils.snackbarText("訂購成功");
            ARouter.getInstance().build(RouterHub.SALES_CLIENT_PAY_ACTIVITY).withString("order_no", orderBuy.getData().getOrder_no()).navigation();
        }
    }


    private void setBannerData(ProductDetail.DataBean dataBean) {
        List<String> images = new ArrayList();
        List<String> titles = new ArrayList();

        images.add(dataBean.getProduct_pic1());
        images.add(dataBean.getProduct_pic2());
        images.add(dataBean.getProduct_pic3());
        titles.add(dataBean.getProduct_name());
        titles.add(dataBean.getProduct_name());
        titles.add(dataBean.getProduct_name());

        //设置banner样式
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(images);
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.DepthPage);
        //设置标题集合（当banner样式有显示title时）
        banner.setBannerTitles(titles);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(1500);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }


    @OnClick({R2.id.btn_ding_gou, R2.id.btn_qu_xiao})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.btn_ding_gou) {

            if (mSharedPreferencesHelper.getString(BaseConstants.IS_STAFF).equals("0")) {//0是用户，1是员工
                mPresenter.orderBuy(product_id, mSharedPreferencesHelper.getString(BaseConstants.TOKEN), String.valueOf(buyNum));
            } else {
                mPresenter.orderBuy(product_id, mSharedPreferencesHelper.getString(BaseConstants.TOKEN), String.valueOf(buyNum), "1");
            }
        } else if (i == R.id.btn_qu_xiao) {
            killMyself();
        }
    }
}
